package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.mapper.NtOldmanTypeIetypeMapper;
import com.csun.cmny.provider.mapper.NtPaymentDerateOthersMapper;
import com.csun.cmny.provider.model.domain.*;
import com.csun.cmny.provider.model.dto.DerateOthersDto;
import com.csun.cmny.provider.model.dto.ExpenseItemDto;
import com.csun.cmny.provider.model.dto.PaymentDerate;
import com.csun.cmny.provider.model.move.MoveOldmanIeType;
import com.csun.cmny.provider.model.move.MoveOldmanTypeIetype;
import com.csun.cmny.provider.model.move.MovePaymentDerate;
import com.csun.cmny.provider.model.move.MoveUserType;
import com.csun.cmny.provider.service.*;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class NtOldmanTypeIetypeServiceImpl extends BaseService<NtOldmanTypeIetype> implements NtOldmanTypeIetypeService{

    @Resource
    private NtOldmanTypeIetypeMapper ntOldmanTypeIetypeMapper;
    @Resource
    private NtHomeService ntHomeService;
    @Resource
    private NtOldmanTypeService ntOldmanTypeService;
    @Resource
    private OmcDataMoveFeighApi omcDataMoveFeighApi;
    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private NtOldmanIeTypeItemService ntOldmanIeTypeItemService;
    @Resource
    private NtOldmanService ntOldmanService;
    @Resource
    private NtPaymentDerateOthersService ntPaymentDerateOthersService;

    @Override
    public int moveOldmanTypeToCmny() {

        //机构
        List<NtHome> ntHomes = ntHomeService.selectAll();

        //人员类别
        List<NtOldmanType> ntOldmanTypes = ntOldmanTypeService.selectAll();

        List<MoveUserType> moveUserTypeList = Lists.newArrayList();

        ntHomes.forEach(ntHome -> {
            ntOldmanTypes.forEach(type -> {
                MoveUserType userType = new MoveUserType();
                userType.setUserTypeName(type.getTypename());
                userType.setUserTypeCode(type.getTypecode());
                userType.setRemark(type.getTypedesc());
                userType.setLastOperatorId(type.getModifier());
                userType.setGroupId(ntHome.getHomeid().longValue());
                moveUserTypeList.add(userType);
            });
        });

        return omcDataMoveFeighApi.moveOldmanTypeToCmny(moveUserTypeList);
    }

    @Override
    public int movePeopleTypeSubstractRate() {
        List<NtOldmanTypeIetype> ntOldmanTypeIetypeList = selectAll();
        List<MoveOldmanTypeIetype> moveUserTypeList = Lists.newArrayList();
        ntOldmanTypeIetypeList.forEach(o -> {
            MoveOldmanTypeIetype oldmanTypeIetype = new MoveOldmanTypeIetype();
            BeanUtils.copyProperties(o,oldmanTypeIetype);
            oldmanTypeIetype.setChangetime(null);
            oldmanTypeIetype.setCreatetime(null);
            oldmanTypeIetype.setModifytime(null);
            moveUserTypeList.add(oldmanTypeIetype);
        });
        return omcRpcService.movePeopleTypeSubstractRate(moveUserTypeList);
    }

    @Override
    public int initExpenseConfig() {
        //查询配置初始化的机构
        List<NtHome> ntHomes = ntHomeService.selectAll();
        List<Long> groupIdList = ntHomes.stream().map(h -> h.getHomeid().longValue()).collect(Collectors.toList());
        return omcRpcService.initExpenseConfig(groupIdList);
    }

    @Override
    public int moveOldmanIn() {
        List<NtOldman> ntOldmanList = ntOldmanService.selectAll();
        for (NtOldman o : ntOldmanList) {
            //查询老人消费项目信息、减免信息
            OldMan oldMan = ntOldmanService.selectById(o.getId());
//            OldMan oldMan = ntOldmanService.selectById(383);
//            NtOldman o = ntOldmanService.selectByKey(383);
            Integer oldmanId = oldMan.getId();
            DerateOthersDto derateOthersDto = ntPaymentDerateOthersService.queryAll(oldmanId, 0);

            //传输对象
            MoveOldmanIeType oldmanIeType = new MoveOldmanIeType();

            //查询老人私人定制项目
            List<NtOldmanIeTypeItem> oldmanIeTypeItemList = ntOldmanIeTypeItemService.selectOldmanIeTypeItem(oldmanId);
            List<String> privateItemNameList = oldmanIeTypeItemList.stream().map(oi -> oi.getItemName()).collect(Collectors.toList());

            //拷贝属性
            BeanUtils.copyProperties(oldMan.getOldManIeType(), oldmanIeType);
            List<MovePaymentDerate> movePaymentDerateList = Lists.newArrayList();
            if (derateOthersDto != null) {
                List<PaymentDerate> paymentDerateList = derateOthersDto.getList();
                for (PaymentDerate p : paymentDerateList) {
                    MovePaymentDerate movePaymentDerate = new MovePaymentDerate();
                    p.setCreateTime(null);
                    BeanUtils.copyProperties(p, movePaymentDerate);
                    movePaymentDerateList.add(movePaymentDerate);
                }
            }
            oldmanIeType.setPrivateItemNameList(privateItemNameList);
            if (o.getHomeid() != null && o.getIspayed().equals(1)) {
                //放入机构Id
                oldmanIeType.setGroupId(o.getHomeid().longValue());
                oldmanIeType.setOldManId(oldmanId);
                oldmanIeType.setManualList(movePaymentDerateList);
                if (oldMan.getOldManIeType() != null) {
                    omcRpcService.moveOldmanIn(oldmanIeType);
                }
            }
        }
            return 0;
    }

        @Override
        public int dccDsinfectionAndMedicPlan () {
            return 0;
        }

        @Override
        public int moveOldmanInItem () {
            List<NtOldman> ntOldmanList = ntOldmanService.selectAll();
            for (NtOldman o : ntOldmanList) {
//                NtOldman o = ntOldmanService.selectByKey(383);
                OldMan oldMan = ntOldmanService.selectById(o.getId());
                //传输对象
                MoveOldmanIeType oldmanIeType = new MoveOldmanIeType();
                //拷贝属性
                BeanUtils.copyProperties(oldMan.getOldManIeType(), oldmanIeType);
                if (oldmanIeType != null && oldMan.getIsPayed().equals(1)) {
                    oldmanIeType.setOldManId(oldMan.getId());
                    oldmanIeType.setGroupId(o.getHomeid().longValue());
                    oldmanIeType.setIsPay(oldMan.getIsPayed());
                    oldmanIeType.setInDate(oldMan.getInDate());
                    omcRpcService.moveOldmanInItem(oldmanIeType);
                }
            }
            return 0;
        }
}
